/**
 * Copyright (C) 2025 pony working team
 * SPDX-License-Identifier: MulanPSL-2.0
 */

#include <gtest/gtest.h>
#include <cstdio>

#include <x_config.h>
#include <crc.h>

class TestCRC : public ::testing::Test
{
protected:
    void SetUp() {};

    void TearDown() {};
};

#ifdef CONFIG_CRC8
static void print_crc8_table(uint8_t table[256])
{
    int i;

    for (i = 0; i < 256; i++) {
        printf("0x%02x ", table[i]);
        if ((i + 1) % 16 == 0) {
            printf("\n");
        }
    }
    printf("\n");
}

TEST_F(TestCRC, crc8)
{
    uint8_t table[256] = {0};
    crc8_gen_table(table);
    printf("crc8_gen_table:\n");
    print_crc8_table(table);

    uint8_t val = crc8_calc(CRC_TEST_DATA, strlen(CRC_TEST_DATA));
    printf("crc8 crc: 0x%02x\n", val);
}
#endif

#if defined(CONFIG_CRC16) || defined(CONFIG_CRC_CCITT)
static void print_crc16_table(uint16_t table[256])
{
    int i;

    for (i = 0; i < 256; i++) {
        printf("0x%04x ", table[i]);
        if ((i + 1) % 8 == 0) {
            printf("\n");
        }
    }
    printf("\n");
}
#endif

#ifdef CONFIG_CRC16
TEST_F(TestCRC, crc16)
{
    uint16_t table[256] = {0};
    crc16_gen_table(table);
    printf("crc16_gen_tablele:\n");
    print_crc16_table(table);

    uint16_t val = crc16_calc(CRC_TEST_DATA, strlen(CRC_TEST_DATA));
    printf("crc16 crc: 0x%04x\n", val);
    val = crc16_modbus(CRC_TEST_DATA, strlen(CRC_TEST_DATA));
    printf("crc16_modbus crc: 0x%04x\n", val);
}
#endif

#ifdef CONFIG_CRC_CCITT
TEST_F(TestCRC, crc_ccitt)
{
    uint16_t table[256] = {0};
    crc_ccitt_gen_table(table);
    printf("crc_ccitt_gen_table:\n");
    print_crc16_table(table);

    uint16_t val = crc_ccitt(CRC_TEST_DATA, strlen(CRC_TEST_DATA));
    printf("crc_ccitt crc: 0x%04x\n", val);
    val = crc_xmodem(CRC_TEST_DATA, strlen(CRC_TEST_DATA));
    printf("crc_xmodem crc: 0x%04x\n", val);
    val = crc_ccitt_pcp(CRC_TEST_DATA, strlen(CRC_TEST_DATA));
    printf("crc_ccitt_pcp crc: 0x%04x\n", val);
}
#endif

#ifdef CONFIG_CRC32
static void print_crc32_table(uint32_t table[256])
{
    int i;

    for (i = 0; i < 256; i++) {
        printf("0x%08x ", table[i]);
        if ((i + 1) % 8 == 0) {
            printf("\n");
        }
    }
    printf("\n");
}

TEST_F(TestCRC, crc32)
{
    uint32_t table[256] = {0};
    crc32_gen_table(table);
    printf("crc32_gen_table:\n");
    print_crc32_table(table);

    uint32_t val = crc32_calc(CRC_TEST_DATA, strlen(CRC_TEST_DATA));
    printf("crc32 crc: 0x%08x\n", val);
}
#endif

#ifdef CONFIG_CRC64
static void print_crc64_table(uint64_t table[256])
{
    int i;

    for (i = 0; i < 256; i++) {
        printf("0x%016lx ", table[i]);
        if ((i + 1) % 4 == 0) {
            printf("\n");
        }
    }
    printf("\n");
}

TEST_F(TestCRC, crc64)
{
    uint64_t table[256] = {0};
    crc64_gen_table(table);
    printf("crc64_gen_table:\n");
    print_crc64_table(table);

    uint64_t val = crc64_ecma(CRC_TEST_DATA, strlen(CRC_TEST_DATA));
    printf("crc64_ecma crc: 0x%016lx\n", val);
    val = crc64_we(CRC_TEST_DATA, strlen(CRC_TEST_DATA));
    printf("crc64_we crc: 0x%016lx\n", val);
}
#endif
